package com.yanqu.road.dao.impl.activity.langyabang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.langyabang.data.LangYaUnionData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class LangYaBangUnionDataDaoImpl extends TempDataObjectDao<LangYaUnionData> {
    public static String getLangYaUnionDataSql = "select * from `t_c_lang_ya_union_data` where `activityId`=? and `groupId`=?";
    public Map<String,LangYaUnionData> getLangYaUnionData(int activityId,int groupId) {
        Map<String,LangYaUnionData> unionDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getLangYaUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaUnionData unionData = parse(rs);
                unionDataMap.put(unionData.getUnionUid(),unionData) ;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return unionDataMap;
    }

    public static String updateLangYaUnionDataSql = "update `t_c_lang_ya_union_data` set userIdSet=?,scoreNow=?,hateValueMap=? where activityId=? and groupId=? and unionUid=?";
    @Override
    protected boolean updateData(LangYaUnionData langYaUnionData) {
        langYaUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaUnionData.getUserIdSet())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaUnionData.getScoreNow()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaUnionData.getHateValueMap())));
        para.put(i++, new DbParameter(Types.INTEGER, langYaUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, langYaUnionData.getUnionUid()));
        if (getDb().executeNoneQuery(updateLangYaUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String insertLangYaUnionDataSql = "insert into `t_c_lang_ya_union_data`(activityId,groupId,unionUid,userIdSet,scoreNow,hateValueMap) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(LangYaUnionData langYaUnionData) {
        langYaUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, langYaUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, langYaUnionData.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaUnionData.getUserIdSet())));
        para.put(i++, new DbParameter(Types.BIGINT, langYaUnionData.getScoreNow()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaUnionData.getHateValueMap())));
        if (getDb().executeNoneQuery(insertLangYaUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected LangYaUnionData parseData(ResultSet rs) throws SQLException {
        LangYaUnionData langYaUnionData = new LangYaUnionData();
        langYaUnionData.setActivityId(rs.getInt("activityId"));
        langYaUnionData.setGroupId(rs.getInt("groupId"));
        langYaUnionData.setUnionUid(rs.getString("unionUid"));
        langYaUnionData.setUserIdSet(JSON.parseObject(rs.getString("userIdSet"), Set.class));
        if (langYaUnionData.getUserIdSet() == null){
            langYaUnionData.setUserIdSet(new HashSet<>());
        }
        langYaUnionData.setScoreNow(rs.getLong("scoreNow"));
        langYaUnionData.setHateValueMap(JSON.parseObject(rs.getString("hateValueMap"), new TypeReference<ConcurrentHashMap<String, Integer>>() {}));
        if (langYaUnionData.getHateValueMap() == null){
            langYaUnionData.setHateValueMap(new ConcurrentHashMap<>());
        }
        return langYaUnionData;
    }


}
